<?php
class dashboard extends MY_Controller{
	
	public $title,$id_member;
	
	function dashboard(){
		parent::__construct();
//		$this->load->library('');
//		$this->load->library('session');
//		$this->validateCookie();
		$this->id_member = get_cookie('member');
	}
	
	
	function index(){
		$true = md5('success');
    	if(get_cookie('logged')== $true){
    		$this->wall();
    	} else {
    		$this->signup();
    	}
		
	}
	
	function signup(){
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("theme/default/dashboard/signup/header",$header,true);
		
		/*
		 * load content
		 */
		$content = $this->config->item("content");
		$content['main'] = $this->load->view('theme/default/dashboard/signup/lpanel','',true);
		$content['lpanel'] = $this->load->view('theme/default/dashboard/signup/main','',true);
//		$content['rpanel'] = $this->load->view('restaurant/profile/theme/default/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	function wall(){
		$this->validateCookie();
		$this->title = "Home";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		
		$this->load->model("imagesMod");
		$query = $this->imagesMod->get_order(0,7);
		
		/*
		 * load content
		 */
//		$content = $this->config->item("content");
		$content['lpanel'] = $this->load->view('theme/default/dashboard/home/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/home/wall/wall',array('query'=>$query),true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/home/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=> $con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	function newmember(){
		$this->load->model("memberMod");
		$this->load->model("infoMod");
		$this->load->model("contactMod");
		
		/*
		 * table memberMod
		 */
		$username = $this->input->post("username");
		$password = md5($this->input->post("password"));
		$id_member = $this->memberMod->generateID($username);
		
		/*
		 * table infoMod
		 */
		$first_name = $this->input->post("first_name");
		$last_name = $this->input->post("last_name");		
		
		/*
		 * table contactMod
		 */
		$email = $this->input->post("email");
		
		$memberMod = array(
      		"id_member"		=> $id_member,
        	"username"		=> $username, 
        	"password"		=> $password,
			"images" 		=> "",
			"thumbs"		=> "",
			"type_images"	=> "",
      	);
      	$infoMod = array(
      		"id_member" => $id_member,
			"first_name" => $first_name,
			"last_name" => $last_name,
			"bio" => "",
			"address" => "",
			"city" => "",
      	);
      	$contactMod = array(
      		"id_member" => $id_member,
			"email" => $email,
			"phone" => "",
			"website" => "",
      	);
      	
		
		$this->memberMod->Add($memberMod);
		$this->infoMod->Add($infoMod);
		$this->contactMod->Add($contactMod);
		
		header("Location: ../");
		return true;
	}
	
	function info(){
		$this->validateCookie();
		$this->title = "Info";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		
		$this->load->model('memberMod');
		$this->load->model('infoMod');
		$this->load->model('contactMod');
		
		$member = $this->memberMod->get($this->id_member);
		$info = $this->infoMod->get($this->id_member);
		$contact = $this->contactMod->get($this->id_member);
		
		$query = array(
			'id'=> $this->id_member,
			'member' => $member,
			'basic' => $info,
			'info' => $info,
			'contact' => $contact
		);
		
		/*
		 * load content
		 */
//		$content = $this->config->item("content");
		$content['lpanel'] = $this->load->view('theme/default/dashboard/home/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/home/info/info',$query,true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/home/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=> $con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}

	function project(){
		$this->validateCookie();
		$this->title = "Project";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		
		/*
		 * load content
		 */
//		$content = $this->config->item("content");
		$content['lpanel'] = $this->load->view('theme/default/dashboard/home/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/home/project/','',true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/home/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=> $con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	/* 
	 * ---------------------------------------------------------------------------------------------------- add
	 */
	
	function addEditProfile(){
		$this->title = "Edit Profile";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		
		/*
		 * load content
		 */
		$content = $this->config->item("content");
//		$content['lpanel'] = $this->load->view('restaurant/profile/theme/default/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/home/info/addEditProfile',array('id_member'=>$this->id_member),true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/home/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	/* 
	 * ---------------------------------------------------------------------------------------------------- show
	 */
	
	function showContact(){
		$this->validateCookie();
		$this->load->model('contactMod');
		$id = $this->uri->segment(3,0);
//		echo $id;
		$query = $this->contactMod->get($id);
		$this->load->view('theme/default/dashboard/home/info/showContact',array('contact'=>$query));
		return true;
	}
	
	function showBasic(){
		$this->load->model('infoMod');
		$id = $this->uri->segment(3,0);
//		echo $id;
		$query = $this->infoMod->get($id);
		$this->load->view('theme/default/dashboard/home/info/showBasic',array('basic'=>$query));
		return true;
	}
	
	function showAddress(){
		$this->load->model('infoMod');
		$id = $this->uri->segment(3,0);
//		echo $id;
		$query = $this->infoMod->get($id);
		$this->load->view('theme/default/dashboard/home/info/showAddress',array('info'=>$query));
		return true;
	}
	
	/*
	 * ---------------------------------------------------------------------------------------------------- load
	 */
	function loadThumbs(){
        $id = $this->uri->segment(3,0);
//      	echo $id;
        $this->load->model('memberMod');
        $query = $this->memberMod->getThumbs($id);
        $this->load->view('theme/default/dashboard/home/info/loadThumbs',array('query'=>$query));
//			$this->load->view('theme/default/dashboard/home/info/loadThumbs');
//        $this->load->view('theme/default/dashboard/project/images/loadThumbs',$query);
    }
	
	/*
	 * ---------------------------------------------------------------------------------------------------- edit
	 */	
	function editBasic(){
//		 $id = $this->uri->segment(3,0);
//		 echo $id;
		$id = $this->input->post('dt');
//		echo $id;
//		echo "osdugkshlsdsd";
        $this->load->model('infoMod');
        $query = $this->infoMod->get($id);
        $this->load->view('theme/default/dashboard/home/info/editBasicInfo',array('query'=>$query));
	}
	
	function editAddress(){
//		 $id = $this->uri->segment(3,0);
//		 echo $id;
		$id = $this->input->post('dt');
//		echo $id;
//		echo "osdugkshlsdsd";
        $this->load->model('infoMod');
        $query = $this->infoMod->get($id);
        $this->load->view('theme/default/dashboard/home/info/editAddress',array('query'=>$query));
	}
	
	function editContact(){
//		 $id = $this->uri->segment(3,0);
//		 echo $id;
		$id = $this->input->post('dt');
//		echo $id;
//		echo "osdugkshlsdsd";
        $this->load->model('contactMod');
        $query = $this->contactMod->get($id);
        $this->load->view('theme/default/dashboard/home/info/editContact',array('query'=>$query));
	}
	
	/*
	 * ---------------------------------------------------------------------------------------------------- save
	 */
	function saveEditProfile(){
		$this->load->model('memberMod');
//		$this->load->model('albumMod');
//		$id_album = $this->uri->segment(3,0);
//		$id_images = $this->uri->segment(4,0);
		$picture_tmp = $_FILES['images']['tmp_name'];
		$picture_name = $_FILES['images']['name'];
		$compress = 400;
		$thumbWidth = 120;
		$images_type = $_FILES['images']['type'];
//		$id = $this->input->post('id_member');
		
			$image = "";
			$newData = array(
				"images" => $this->images($picture_tmp,$compress,$picture_name),
				"thumbs" => $this->thumbs($picture_tmp,$thumbWidth,$picture_name),
				"type_images" => $images_type,
	      	);
		
		$this->memberMod->update($this->id_member,$newData);
		redirect('dashboard/info');
		return true;
	}
	
	function saveEditBasic(){
		$this->load->model('infoMod');
//		$this->load->model('albumMod');
//		$id_album = $this->uri->segment(3,0);
//		$id_images = $this->uri->segment(4,0);
		$id = $this->input->post('id_member');
		$fn = $this->input->post('first_name');
		$ln = $this->input->post('last_name');
		$bio = $this->input->post('bio');
		$data = array(
			'first_name' => $fn,
			'last_name' => $ln,
			'bio' => $bio,
		);
		
		$this->infoMod->update($id,$data);
		
	}
	
	function saveEditAddress(){
		$this->load->model('infoMod');
//		$this->load->model('albumMod');
//		$id_album = $this->uri->segment(3,0);
//		$id_images = $this->uri->segment(4,0);
		$id = $this->input->post('id_member');
		$fn = $this->input->post('address');
		$ln = $this->input->post('city');
		$data = array(
			'address' => $fn,
			'city' => $ln,
		);
		
		$this->infoMod->update($id,$data);
		
	}
	
	function saveEditContact(){
		$this->load->model('contactMod');
//		$this->load->model('albumMod');
//		$id_album = $this->uri->segment(3,0);
//		$id_images = $this->uri->segment(4,0);
		$id = $this->input->post('id_member');
		$email = $this->input->post('email');
		$phone = $this->input->post('phone');
		$website = $this->input->post('website');
		$data = array(
			'email' => $email,
			'phone' => $phone,
			'website' => $website,
		);
		
		$this->contactMod->update($id,$data);
		
	}
	
	/*
	 * ---------------------------------------------------------------------------------------------------- images
	 */
	function images($picture_tmp,$compress,$picture_name){
		$pic = imagecreatefromjpeg($picture_tmp);
		$width_comp  = imagesx( $pic );
		$height_comp  = imagesy( $pic );
		if($width_comp<300){
			$create = $pic;
			$new_width_comp = $width_comp;
			$new_height_comp  = $height_comp;
			$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
			$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
			$pth_comp  = site_url("images/album/$picture_name");
			ob_start();
			$inp_comp  = imagejpeg( $tmp_img_comp  );
			$create = ob_get_contents();	
			return $create;	
		} else {
		$new_width_comp = $compress;
		$new_height_comp  = floor( $height_comp * ( $compress / $width_comp  ) );
		$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
		$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
		$pth_comp  = site_url("images/album/$picture_name");
		ob_start();
		$inp_comp  = imagejpeg( $tmp_img_comp  );
		$create = ob_get_contents(); 
		return $create;
		
		}
		
	
	}
	
	function thumbs($picture_tmp,$thumbwidth,$picture_name){
		$thumb = imagecreatefromjpeg($picture_tmp);
		$width = imagesx( $thumb );
		$height = imagesy( $thumb );
		$new_width = $thumbwidth;
		$new_height = floor( $height * ( $thumbwidth / $width ) );
		$tmp_img = imagecreatetruecolor( $new_width, $new_height );
		$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
		$pth = site_url("images/album/thumbs/$picture_name");
		ob_start();
		$inp = imagejpeg( $tmp_img );
		$c = ob_get_contents(); 
		return $c;
	}
	
}